unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBXpress, FMTBcd, StdCtrls, DB, SqlExpr, jpeg, ExtCtrls,
  DBClient, ComCtrls, Grids, DBGrids, DBCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    Image1: TImage;
    Button2: TButton;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    CheckBox1: TCheckBox;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    Button14: TButton;
    Memo1: TMemo;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    Button20: TButton;
    Button21: TButton;
    Button22: TButton;
    Button23: TButton;
    Button24: TButton;
    Button25: TButton;
    SQLConnection1: TSQLConnection;
    SQLQuery1: TSQLQuery;
    DataSource1: TDataSource;
    TabSheet2: TTabSheet;
    DBGrid1: TDBGrid;
    Button26: TButton;
    Button27: TButton;
    Edit1: TEdit;
    DBMemo1: TDBMemo;
    Button28: TButton;
    Button29: TButton;
    Button30: TButton;
    Button31: TButton;
    SetOld: TButton;
    SetNew: TButton;
    Label2: TLabel;
    Button32: TButton;
    SQLMonitor1: TSQLMonitor;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure Button17Click(Sender: TObject);
    procedure Button18Click(Sender: TObject);
    procedure Button19Click(Sender: TObject);
    procedure Button20Click(Sender: TObject);
    procedure Button21Click(Sender: TObject);
    procedure Button22Click(Sender: TObject);
    procedure Button23Click(Sender: TObject);
    procedure Button24Click(Sender: TObject);
    procedure Test(Sender: TObject);
    procedure Button25Click(Sender: TObject);
    procedure Button26Click(Sender: TObject);
    procedure Button27Click(Sender: TObject);
    procedure Button28Click(Sender: TObject);
    procedure Button29Click(Sender: TObject);
    procedure Button30Click(Sender: TObject);
    procedure Button31Click(Sender: TObject);
    procedure SetOldClick(Sender: TObject);
    procedure SetNewClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button32Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure TestType(ColumnName : string; ColumnType : TFieldType; Value : variant);
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

const
  SQL = 'update regress set %s = :value';

{$R *.dfm}

procedure TForm1.TestType(ColumnName: string; ColumnType: TFieldType;
  Value: variant);
begin
  SQLQuery1.SQL.Text := Format(SQL, [ColumnName]);

  if CheckBox1.Checked then
  begin
    SQLQuery1.Params[0].Clear;
    SQLQuery1.Params[0].DataType := ColumnType;
    SQLQuery1.Params[0].Bound := True;
  end else
  begin
    SQLQuery1.Params[0].DataType := ColumnType;
    SQLQuery1.Params[0].Value := Value;
  end;

  try
    Label1.Caption := SQLQuery1.Params[0].AsString;
  except
  end;

  SQLQuery1.ExecSQL();
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  TestType('c_char', ftFixedChar, Now);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  TestType('c_smallint', ftSmallint, Random(64000)-32000);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  TestType('c_int', ftInteger, Random(1000000) - 1000000 div 2);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  TestType('c_float', ftFloat, 10000*(Random - 0.5));
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  TestType('c_smallfloat', ftFloat, 10000*(Random - 0.5));
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  TestType('c_decimal_fix', ftBCD, 10000*(Random - 0.5));
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  Bcd: TBcd;
begin
  Bcd.Precision := 20;
  Bcd.SignSpecialPlaces := 19;
  Bcd.Fraction[0] := $12;
  Bcd.Fraction[1] := $34;
  Bcd.Fraction[2] := $56;
  Bcd.Fraction[3] := $78;
  Bcd.Fraction[4] := $90;

  Bcd.Fraction[5] := $12;
  Bcd.Fraction[6] := $34;
  Bcd.Fraction[7] := $56;
  Bcd.Fraction[8] := $78;
  Bcd.Fraction[9] := $91;

  SQLQuery1.SQL.Text := Format(SQL, ['c_decimal_flt']);
  SQLQuery1.Params[0].AsFMTBCD := Bcd;;
  try
    Label1.Caption := SQLQuery1.Params[0].AsString;
  except
  end;
  SQLQuery1.ExecSQL();
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
  SQLQuery1.SQL.Text := 'insert into regress (c_serial) values (:value)';
  SQLQuery1.Params[0].DataType := ftAutoInc;
  //SQLQuery1.Params[0].Value := Random(1000000) - 1000000 div 2;
  SQLQuery1.Params[0].Value := 0;
  try
    Label1.Caption := SQLQuery1.Params[0].AsString;
  except
  end;
  SQLQuery1.ExecSQL();
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
  TestType('c_date', ftDate, Now);
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
  TestType('c_money', ftCurrency, 10000*(Random - 0.5));
end;

procedure TForm1.Button12Click(Sender: TObject);
begin
  TestType('c_time', ftTime, Now);
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
  TestType('c_datetime', ftDateTime, Now);
end;

procedure TForm1.Button13Click(Sender: TObject);
var
  T: TDateTime;
begin
  T := Now;
  TestType('c_datetime', ftTimeStamp, T);
  Label1.Caption := FormatDateTime('yyyy.mm.dd hh.nn.ss.zzz', T);
end;

procedure TForm1.Button14Click(Sender: TObject);
var
  S: TMemoryStream;
begin
  SQLQuery1.SQL.Text := Format(SQL, ['c_bytes']);

  if CheckBox1.Checked then
  begin
    SQLQuery1.Params[0].Clear;
    SQLQuery1.Params[0].DataType := ftBlob;
    SQLQuery1.Params[0].Bound := True;
  end else
  begin
    SQLQuery1.Params[0].DataType := ftBlob;
    S := TMemoryStream.Create;
    with S do
      try
        Memo1.Lines.SaveToStream(S);
        SQLQuery1.Params[0].SetBlobData(S.Memory, S.Size);
      finally
        Free;
      end;
  end;

  try
    Label1.Caption := SQLQuery1.Params[0].AsString;
  except
  end;

  SQLQuery1.ExecSQL();
end;

procedure TForm1.Button15Click(Sender: TObject);
begin
  TestType('c_text', ftMemo, Memo1.Lines.Text);
end;

procedure TForm1.Button16Click(Sender: TObject);
begin
  TestType('c_varchar', ftString, Now);
end;

procedure TForm1.Button17Click(Sender: TObject);
begin
  TestType('c_interval', ftString, '12 10:10:10');
end;

procedure TForm1.Button18Click(Sender: TObject);
begin
  TestType('c_nchar', ftFixedChar, Now);
end;

procedure TForm1.Button19Click(Sender: TObject);
begin
  TestType('c_nvarchar', ftString, Now);
end;

procedure TForm1.Button20Click(Sender: TObject);
begin
  TestType('c_boolean', ftBoolean, True);
end;

procedure TForm1.Button21Click(Sender: TObject);
begin
  SQLConnection1.Open;
  TestType('c_clob', ftOraClob, Memo1.Lines.Text);
end;

procedure TForm1.Button22Click(Sender: TObject);
begin
  SQLConnection1.Open;
  TestType('c_blob', ftOraBlob, Memo1.Lines.Text);
end;

procedure TForm1.Button23Click(Sender: TObject);
begin
  SQLConnection1.Open;
  TestType('c_lvarchar', ftFmtMemo, Memo1.Lines.Text);
end;

procedure TForm1.Button24Click(Sender: TObject);
var
  S: TMemoryStream;
begin
  SQLQuery1.SQL.Text := Format(SQL, ['c_bytes']);

  if CheckBox1.Checked then
  begin
    SQLQuery1.Params[0].Clear;
    SQLQuery1.Params[0].DataType := ftGraphic;
    SQLQuery1.Params[0].Bound := True;
  end else
  begin
    S := TMemoryStream.Create;
    with S do
      try
        Image1.Picture.Graphic.SaveToStream(S);
        SQLQuery1.Params[0].LoadFromStream(S, ftGraphic);
      finally
        Free;
      end;
  end;
  SQLQuery1.ExecSQL();
end;

procedure TForm1.Test(Sender: TObject);
var
  Value: Variant;
begin
  SQLConnection1.Open;
  Value := VarArrayCreate([0, 10], varByte);
  Value[0] := 0;
  Value[1] := 1;
  Value[2] := 2;
  Value[3] := 3;
  Value[4] := 4;
  Value[5] := 5;
  Value[6] := 6;
  Value[7] := 7;
  Value[8] := 8;
  Value[9] := 9;
  TestType('c_row', ftBytes, Value);
end;

procedure TForm1.Button25Click(Sender: TObject);
begin
  SQLConnection1.Open;
  TestType('c_row', ftFmtMemo, 'ROW(2, ''sadf'')');
end;

procedure TForm1.Button26Click(Sender: TObject);
begin
{  SimpleDataSet1.Close;
  with TStringList.Create do
    try
      LoadFromFile(ExtractFilePath(Application.ExeName) + '\test.sql');
      SimpleDataSet1.DataSet.CommandText := Text;
    finally
      Free;
    end;
  SimpleDataSet1.Open;}
end;

procedure TForm1.Button27Click(Sender: TObject);
begin
  //SimpleDataSet1.Close;
end;

procedure TForm1.Button28Click(Sender: TObject);
begin
  DBMemo1.DataField := Edit1.Text;
end;

procedure TForm1.Button29Click(Sender: TObject);
var
  I: Integer;
  //S1, S2: string;
  dw1, dw2: DWORD;
  Sum: DWORD;
  Command: ISQLCommand;
  Cursor: ISQLCursor;
begin
  SQLQuery1.SQL.LoadFromFile(ExtractFilePath(Application.ExeName) + '\test.sql');
  SQLConnection1.Open;
  Sum := 0;
  for I := 0 to 5 do
  begin
    {
    SQLConnection1.SQLConnection.getSQLCommand(Command);
    Command.prepare(SQLQuery1.SQL.GetText, 0);
    dw1 := GetTickCount;
    Command.execute(Cursor);
    while Cursor.next = SQL_SUCCESS do ;
    Cursor := nil;
    dw2 := GetTickCount;
    Inc(Sum, dw2 - dw1);
    {}
    SQLQuery1.Open;
    while not SQLQuery1.Eof do
    begin
      SQLQuery1.Next;
    end;
    SQLQuery1.Close;
    SQLQuery1.Prepared := False;
    {}
  end;
    Command := nil;
  SQLConnection1.Close;
  ShowMessage(IntToStr(Sum));
end;

procedure TForm1.Button30Click(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to 100 do
    Button23Click(Sender);
    //Button21Click(Sender);
end;

procedure TForm1.Button31Click(Sender: TObject);
begin
  SQLConnection1.Open;
end;

procedure TForm1.SetOldClick(Sender: TObject);
begin
  SQLConnection1.Close;
  SQLConnection1.LibraryName := 'lxdbexpinf.dll.old';
  Label2.Caption := 'Old';
end;

procedure TForm1.SetNewClick(Sender: TObject);
begin
  SQLConnection1.Close;
  SQLConnection1.LibraryName := 'lxdbexpinf.dll';
  Label2.Caption := 'New';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //SetNew.Click;
end;

procedure TForm1.Button32Click(Sender: TObject);
begin
  //SimpleDataSet1.ApplyUpdates(0);
end;

end.
